package com.liu.wode.controller;

import com.liu.wode.mapper.RoomSortMapper;
import com.liu.wode.mapper.RoomsMapper;
import com.liu.wode.pojo.RoomSort;
import com.liu.wode.pojo.Rooms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class RoomsController {
    @Autowired
    RoomsMapper roomsMapper;
    @Autowired
    RoomSortMapper roomSortMapper;

    @GetMapping("/Lrooms")
    public String ListRoom(Model model){
        List<Rooms> rooms = roomsMapper.queryRoomsList();
        model.addAttribute("rooms",rooms);
        return "room/rlist";
    }

    @GetMapping("/RtoAdd")
    public String toAddUser(Model model){
        List<RoomSort> roomSorts = roomSortMapper.queryRoomSortList();
        model.addAttribute("roomSorts",roomSorts);
        return "room/radd";
    }
    @PostMapping("/RtoAdd")
    public String addUser(@RequestParam("sort") String sort, @RequestParam("price") int price, @RequestParam("number") int number, @RequestParam("active") int active){
        int init=0;
        while (roomsMapper.queryRoomsById(init)!=null){
            init++;
        }
        roomsMapper.addRooms(new Rooms(init,sort,price,number,active));
        return "redirect:/Lrooms";
    }
    @GetMapping("/delRom/{id}")
    public String delUser(@PathVariable("id") Integer id,Model model){
        Rooms room = roomsMapper.queryRoomsById(id);
        if(room.getActive()==0){
            List<Rooms> rooms = roomsMapper.queryRoomsList();
            model.addAttribute("rooms",rooms);
            model.addAttribute("msg","房间信息暂时不可更改");
            return "room/rlist";
        }
        roomsMapper.delRooms(id);
        return "redirect:/Lrooms";
    }

    @GetMapping("/room/{id}")
    public String toUpdateUser(@PathVariable("id")Integer id,Model model){
        Rooms room = roomsMapper.queryRoomsById(id);
        if(room.getActive()==0){
            List<Rooms> rooms = roomsMapper.queryRoomsList();
            model.addAttribute("rooms",rooms);
            model.addAttribute("msg","房间信息暂时不可更改");
            return "room/rlist";
        }
        List<RoomSort> roomSorts = roomSortMapper.queryRoomSortList();
        model.addAttribute("roomSorts",roomSorts);
        model.addAttribute("room",room);
        return "room/rupdate";
    }
    @PostMapping("/updateRoom")
    public String updateUser(Rooms rooms){
        roomsMapper.upRooms(rooms);
        return "redirect:/Lrooms";
    }


}
